package com.shenzhen.horselive.ui.activity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.allenliu.versionchecklib.v2.AllenVersionChecker;
import com.allenliu.versionchecklib.v2.builder.UIData;
import com.bumptech.glide.Glide;
import com.flqy.baselibrary.GlideApp;
import com.flqy.baselibrary.utils.GlideHelper;
import com.flqy.baselibrary.utils.VersionUtils;
import com.liudengjian.imageviewer.util.ImageViewerUtil;
import com.shenzhen.horselive.Constants;
import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.ActivityCollector;
import com.shenzhen.horselive.base.AppBarActivity;
import com.shenzhen.horselive.beans.UpdateBean;
import com.shenzhen.horselive.databinding.ActivitySettingBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.ViewObserver;
import com.shenzhen.horselive.ui.dialog.UpdateDialog;
import com.shenzhen.horselive.utils.DialogManager;
import com.shenzhen.horselive.utils.GlideCacheUtil;

public class SettingActivity extends AppBarActivity implements View.OnClickListener {

    ActivitySettingBinding  binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySettingBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setStyleDark();
        setTitle("设置");
        initViews();
    }

    private void initViews() {
        binding.tvFeedback.setOnClickListener(this);
        binding.btGo.setOnClickListener(this);
        binding.tvHowUse.setOnClickListener(this);
        binding.tvCommQues.setOnClickListener(this);
        binding.tvCheckVersion.setOnClickListener(this);
        binding.tvClearCache.setOnClickListener(this);

        //设置缓存打大小
        binding.tvCache.setText(GlideCacheUtil.getInstance().getCacheSize(getContext()));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.tv_clear_cache:
              new   AlertDialog.Builder(getContext())
                      .setMessage("是否清理缓存？")
                      .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialogInterface, int i) {
                              GlideCacheUtil.getInstance().clearImageMemoryCache(getContext());
                              showLongToast("清理缓存");
                              binding.tvCache.setText("0M");
                          }
                      })
                      .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialogInterface, int i) {

                          }
                      }).show();
                break;
            case R.id.tv_feedback:
                //意见反馈
                startActivity(new Intent(this,FeedbackActivity.class));
                break;
            case R.id.bt_go:
                new AlertDialog.Builder(this)
                        .setMessage("是否确认退出？")
                        .setNegativeButton("取消",null)
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                User.get().loginOut();
                                try {
                                    ActivityCollector.removeAllActivity();
                                    //打开微信登录的页面
                                    startActivity(new Intent(getContext(),LoginActivity.class));

                                }catch (Exception e)
                                {

                                }
                            }
                        })
                      .show()
                ;
                break;
            case R.id.tv_comm_ques:
                //常见问题
                WebActivity.start(this, Constants.H5_COM_PROBLEMS,"常见问题");
                break;
            case R.id.tv_how_use:
                //使用说明
                WebActivity.start(this, Constants.H5_HOW_USE,"使用说明");
                break;
            case R.id.tv_check_version:
                //检查版本更新
                checkVersion();
                break;
        }
    }



    //版本跟新
    private void checkVersion() {
        //0是安卓
        ApiFactory.getApiService().update("0")
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new ViewObserver<UpdateBean>(this) {
                    @Override
                    protected void onNextInActive(UpdateBean updateBean) {
                        if (updateBean.isSuccess())
                        {
                            int  nowVersion = VersionUtils.getCurrentVersionCode(getContext());
                            //版本低于网站版本就下载
                            if (nowVersion<Integer.parseInt(updateBean.getData().getVersionNumber()))
                            {
                                //需要版本更新
                                String  verSionName = "V"+VersionUtils.getCurrentVersion(getContext());
                                String   notice = updateBean.getData().getVersionContent();
                                String  downUrl = updateBean.getData().getDownloadAddress();
                                boolean  isFouce = updateBean.getData().getIsForceUpdate()==1?true:false;
                                DialogManager.getInstance().showUpdateDialog(getContext(), verSionName, notice
                                        , new UpdateDialog.OnUpdateClick() {
                                            @Override
                                            public void onClick() {
                                                // 下载安装
                                                AllenVersionChecker
                                                        .getInstance()
                                                        .downloadOnly(
                                                                UIData.create()
                                                                        .setDownloadUrl(downUrl)
                                                        )
                                                        //直接下载，不弹窗
                                                        .setDirectDownload(true)
                                                        .executeMission(getContext());


                                            }
                                        }, isFouce);

                            }

                        }
                    }
                });
    }


}